<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Categories extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('cmladministration','refresh');
	    }
        $this->load->model('MCategory');
    }
    
    public function index()
    {
        $data['title'] = 'Category List';
		$data['main'] = 'admin/categorylist';
        $data['categories'] = $this->MCategory->getCategoryList();       
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
    
    public function add()
    {
        if($this->input->post('category'))
        {
            $this->MCategory->addCategory();
	  		$this->session->set_flashdata('message','Category added');
	  		redirect('categories','refresh');
        }
        $data['title'] = 'Category Add New';
        $data['main'] = 'admin/categoryadd';    
        $data['form'] = $this->MFormGlobal->getButtonsAdd('categories');   
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
    
    public function edit($id=0)
    {
        if($this->input->post('category'))
        {
            $this->MCategory->updateCategory();
	  		$this->session->set_flashdata('message','Category edited');
	  		redirect('categories','refresh');
        }
        $data['title'] = 'Category Edit';
        $data['category'] = $this->MCategory->getCategoryById($id);
        $data['main'] = 'admin/categoryedit';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('categories');       
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
}